package com.charles.recordapp;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class StartForActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private MeFragment meFragment;
    private Fragment[] fragments;
    private int lastFragment;//用于记录上个选择的Fragment
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //状态栏全透明实现
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//改变状态栏颜色（可以和应用的标题颜色一样）
        }
        setContentView(R.layout.start_for_fragment);
        //权限检查和申请
        initFragment();
    }

    //初始化fragment和fragment数组
    private void initFragment(){
        homeFragment = new HomeFragment();
        meFragment = new MeFragment();
        fragments = new Fragment[]{homeFragment,meFragment};
        lastFragment = 0;
        getSupportFragmentManager().beginTransaction().replace(R.id.lin_lay_fragment,homeFragment).show(homeFragment).commit();
        bottomNavigationView = findViewById(R.id.navigation_view);
        bottomNavigationView.setItemIconTintList(null);
        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
    }
    //判断选择的菜单
    private BottomNavigationView.OnNavigationItemSelectedListener changeFragment= new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            resetToDefaultIcon(item);
            switch (item.getItemId())  {
                case R.id.item_bottom_1: {
                    if(lastFragment!=0) {
                        switchFragment(lastFragment,0);
                        lastFragment=0;
                    }
                    return true;
                }
                case R.id.item_bottom_2: {
                    if(lastFragment!=1) {
                        switchFragment(lastFragment,1);
                        lastFragment=1;
                    }
                    return true;
                }
            }
            return false;
        }
    };

    //切换Fragment
    private void switchFragment(int lastfragment,int index) {
        FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if(fragments[index].isAdded()==false) {
            transaction.add(R.id.lin_lay_fragment,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    private void resetToDefaultIcon(MenuItem item) {
        MenuItem home =  bottomNavigationView.getMenu().findItem(R.id.item_bottom_1);
        home.setIcon(R.mipmap.bottom_item1);
        MenuItem me =  bottomNavigationView.getMenu().findItem(R.id.item_bottom_2);
        me.setIcon(R.mipmap.bottom_item2);
        if (item == home) {
            item.setIcon(R.mipmap.bottom_item1_s);
        } else  {
            item.setIcon(R.mipmap.bottom_item2_s);
        }
    }

}
